<template>
  <div class="search-sugges">
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
  error-text="请求失败，点击重新加载">
      <van-cell v-for="(item, index) in list" :key="index" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search'
export default {
  data() {
    return {
      list: [], // 请求结果数据
      loading: false,
      finished: false,
      // 获取搜索结果参数对象
      query: {
        page: 1,
        per_page: 20,
        q: this.searchText
      },
      error: false
    }
  },
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  created() {},
  mounted() {},
  methods: {
    async onLoad() {
      try {
        const { data } = await getSearchResult(this.query)
        this.list.push(...data.data.results)
        // 模拟错误
        // if (Math.random() >= 0.5) {
        //   JSON.parse('dasdjasd')
        // }
        this.loading = false
        if (data.data.results.length) {
          this.query.page++
        } else {
          this.finished = true
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
